package game;


import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

//just testing svn
/**
 * Canvas Class
 * 
 * @author Group C03
 * @author Mona Khlousy
 * @author Heba El-Samahi 
 * @author Daniel Schobert
 * 
 */

public class LUMM2_Canvas extends GameCanvas implements Runnable, CommandListener {

	/** width of canvas **/
	public static int canvasWidth;

	/** height of canvas **/
	public static int canvasHeight;

	/** LUMM2_Midlet */
	public static LUMM2_Midlet midlet;

	/** key up state **/
	public static boolean up = false;

	/** key down state **/
	public static boolean down = false;

	/** key left state **/
	public static boolean left = false;

	/** key right state **/
	public static boolean right = false;

	/** fire key state **/
	public static boolean fire = false;

	/** right soft key state **/
	public static boolean rsk = false;

	/** left soft key state **/
	public static boolean lsk = false;
	
	/** 1_NUM key state*/
	public static boolean num1 = false;
	
	/** 3_NUM key state*/
	public static boolean num3 = false;

	/** current time **/
	public static long currentTime = 0L;

	/** experiment with this */
	private static final long MAX_GAME_SPEED = 33L;

	// --- do not modify
	private static final byte LEFT_SOFTKEY = -6;
	private static final byte RIGHT_SOFTKEY = -7;
	
	/** Commands */
	public static final Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
	public static final Command CMD_BACK = new Command("Back", Command.BACK, 1);

	// --

	/**
	 * LUMM2_Canvas constructor to init game and start the thread
	 * 
	 * @param mid
	 *            midlet
	 */

	public LUMM2_Canvas(LUMM2_Midlet mid) {
		super(false);
		midlet = mid;
		canvasWidth = getWidth();
		canvasHeight = getHeight();
		
		LUMM2_Game.init(LUMM2_Game.MENU);
		Thread thread = new Thread(this);
		thread.setPriority(Thread.MIN_PRIORITY);
		thread.start();
	}	

	/**
	 * press a key
	 * 
	 * @param key
	 *            keyid
	 */

	protected void keyPressed(int key) {
		switch (mergeKey(key)) {
		case KEY_NUM8:
			down = true;
			break;
		case KEY_NUM4:
			left = true;
			break;
		case KEY_NUM6:
			right = true;
			break;
		case KEY_NUM2:
			up = true;
			break;
		case KEY_NUM5:
			fire = true;
			break;
		case LEFT_SOFTKEY:
			lsk = true;
			break;
		case RIGHT_SOFTKEY:
			rsk = true;
			break;
		case 49: //wenn 1 gedrueckt wurde!
			if(LUMM2_Game.pause) LUMM2_Game.pause=false;
			else LUMM2_Game.pause=true;
			break;
		case 51: //wenn 3 gedrueckt wurde!
			if(LUMM2_Game.mute) LUMM2_Game.mute=false;
			else LUMM2_Game.mute=true;
			break;
		}
	}

	/**
	 * release a key
	 * 
	 * @param key
	 *            keyid
	 */

	protected void keyReleased(int key) {
		switch (mergeKey(key)) {
		case KEY_NUM8:
			down = false;
			break;
		case KEY_NUM4:
			left = false;
			break;
		case KEY_NUM6:
			right = false;
			break;
		case KEY_NUM2:
			up = false;
			break;
		case KEY_NUM5:
			fire = false;
			break;
		case LEFT_SOFTKEY:
			lsk = false;
			break;
		case RIGHT_SOFTKEY:
			rsk = false;
			break;
		}
	}

	/**
	 * merge game keys and normal keys
	 * 
	 * @param key
	 *            keyid
	 * @return mergedkey
	 */

	private byte mergeKey(int key) {
		switch (getGameAction(key)) {
		case DOWN:
			key = KEY_NUM8;
			break;
		case LEFT:
			key = KEY_NUM4;
			break;
		case RIGHT:
			key = KEY_NUM6;
			break;
		case UP:
			key = KEY_NUM2;
			break;
		case FIRE:
			key = KEY_NUM5;
			break;
		}
		return (byte) key;
	}
	
	
	/**
	 * reagiert auf commandos
	 * @param c
	 * 		Command
	 * @param d
	 * 		Displayable
	 * */
	public void commandAction(Command c, Displayable d) {
		if(c==CMD_EXIT){
			midlet.notifyDestroyed();
			midlet.destroyApp(true);
		}else if(c == CMD_BACK){
			if(LUMM2_Game.currentGameState == LUMM2_Game.PLAY){
				LUMM2_Game.reset();
			}
			LUMM2_Game.currentGameState = LUMM2_Game.lastGameState;
			midlet.startApp();
		}
	}

	/**
	 * draw the game
	 * 
	 * @param g
	 *            Graphic Object
	 */

	public void paint(Graphics g) {
		LUMM2_Game.drawGame(g);
		currentTime = System.currentTimeMillis();
	}

	/**
	 * clear the screen
	 * 
	 * @param r
	 *            red
	 * @param g
	 *            green
	 * @param b
	 *            blue
	 * @param g1
	 *            Graphic Object
	 */

	public static void clearScreen(int r, int g, int b, Graphics g1) {
		g1.setColor(r, g, b);
		g1.fillRect(0, 0, canvasWidth, canvasHeight);
	}

	/**
	 * game loop (thread)
	 */

	public void run() {
		try {
			while (true) {
				long executionTime = System.currentTimeMillis() - currentTime;
				if (executionTime < MAX_GAME_SPEED) {
					continue;
				}
				repaint();
				serviceRepaints();
				Thread.yield();
				try {
					Thread.sleep(40);
				} catch (Exception e) {}
			}
		} catch (Exception ex) {
		}

		midlet.notifyDestroyed();
	}
}


